package com.jiezhuo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiezhuo.entity.Menu;
import com.jiezhuo.entity.Role;
import com.jiezhuo.service.MenuService;
import com.jiezhuo.service.RoleService;
import com.jiezhuo.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author com.jiezhuo
 * @since 2022-09-19
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "目录管理")
public class MenuController {
    @Autowired
    MenuService menuService;

    @ApiOperation("添加修改目录")
    @PostMapping("saveOrUpdateMenu")
    public R saveOrUpdateRole(Menu menu) {
        if (StringUtils.isEmpty(menu)) {
            return R.err().msg("输入目录信息").code(4000);
        }
        boolean b = menuService.saveOrUpdate(menu);
        return R.ok().code(2000);
    }

    @ApiOperation("删除目录")
    @PostMapping("deleteMenu")
    public R deleteRole(Integer[] ids) {
        QueryWrapper<Menu> roleQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(ids) || ids.length == 0) {
            return R.err().msg("输入目录信息").code(4000);
        }
        roleQueryWrapper.in("id", ids);
        int delete = menuService.getBaseMapper().delete(roleQueryWrapper);
        return R.ok().code(2000).msg("删除成功");
    }

    @ApiOperation("查询目录")
    @PostMapping("queryMenu")
    public R queryRole(String[] ids, int page, int limit) {
        QueryWrapper<Menu> roleQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(page) || StringUtils.isEmpty(limit)) {
            return R.err().msg("未输入输入page或limit");
        }
        if (!StringUtils.isEmpty(ids) && ids.length > 0) {
            roleQueryWrapper.in("id", ids);
        }
        Page<Menu> rolePage = new Page<>(page, limit);
        Page<Menu> rolePage1 = menuService.getBaseMapper().selectPage(rolePage, roleQueryWrapper);
        return R.ok().code(2000).data("rolePage", rolePage1);
    }

}

